﻿using System.Collections.Generic;
using System.Linq;
using BKO.DataAccesss;
using BKO.DataAccesss.Repositories;
using BKO.Domain.Model;
using BKO.Domain.Repositories;
using BKO.Domain.Services;

namespace BKO.Services
{
	public class TUserService : ITUserService
	{
		private readonly IRepository<ITUser> _tuserRepository;

		public TUserService(string connectionString)
		{
			_tuserRepository = new TUserRepository(BKODataContextSingletone.GetInstance(connectionString));
		}

		public ITUser Get(int id)
		{
			return _tuserRepository.GetAll().SingleOrDefault<ITUser>(u => u.Id == id);
		}

		public List<ITUser> GetAllUsers()
		{
			return _tuserRepository.GetAll().ToList();
		}

		public ITUser AddUser(string userId, string password, string name, string level,
			bool setup = false, bool pcAdd = false, bool pcDel = false, bool capAdd = false,
			bool capDel = false, bool repAdd = false, bool repDel = false, bool repEd = false,
			bool poAdd = false, bool poDel = false, bool po = false, bool cartr = false, bool sclad = false)
		{
			ITUser user = new T_User
				{
					User_ID = userId,
					Password = password,
					Name = name,
					Level = level,
					PCADD = pcAdd,
					PCDEL = pcDel,
					CAPADD = capAdd,
					CAPDEL = capDel,
					REPADD = repAdd,
					REPDEL = repDel,
					REPeD = repEd,
					POADD = poAdd,
					PODEL = poDel,
					PO = po,
					CARTR = cartr,
					SCLAD = sclad
				};
			_tuserRepository.Add(user);
			return user;
		}

		public void UpdateUser(int id, string userId, string password, string name, string level,
			bool setup, bool pcAdd, bool pcDel, bool capAdd, bool capDel, bool repAdd, bool repDel,
			bool repEd, bool poAdd, bool poDel, bool po, bool cartr, bool sclad)
		{
			ITUser user = Get(id);
			user.User_ID = userId;
			user.Password = password;
			user.Name = name;
			user.Level = level;
			user.SETUP = setup;
			user.PCADD = pcAdd;
			user.PCDEL = pcDel;
			user.CAPADD = capAdd;
			user.CAPDEL = capDel;
			user.REPADD = repAdd;
			user.REPDEL = repDel;
			user.REPeD = repEd;
			user.POADD = poAdd;
			user.PODEL = poDel;
			user.PO = po;
			user.CARTR = cartr;
			user.SCLAD = sclad;
			_tuserRepository.Update();
		}

		public ITUser GetUserByName(string name)
		{
			return _tuserRepository.GetAll().SingleOrDefault<ITUser>(u => u.Name == name);
		}

		public void DeleteUser(int id)
		{
			_tuserRepository.Delete(Get(id));
		}
	}
}
